import { getInfoById, getInfoProfile } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {}
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
      setToken(token)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    updateUser(state, userInfo) {
      state.userInfo = userInfo
    },
    delUser(state) {
      state.userInfo = ''
    }

  },
  actions: {
    async  updateUserInfo(context) {
      const res = await getInfoProfile()
      console.log(res, 'getInfoProfile')

      const info = await getInfoById(res.data.userId)
      console.log(info, 'getInfoById')
      context.commit('updateUser', { ...res.data, ...info.data })
    },
    logout(context) {
      context.commit('delToken')
      context.commit('delUser')
    }
  }

}
